﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Xml.Linq;
using Microsoft.Advertising.Mobile.UI;
using HomeBuyerTools.ViewModels;

namespace HomeBuyerTools.Views.HomeValueProjection
{
    public partial class HomeValueProjection : PhoneApplicationPage
    {
        private SolidColorBrush adcontrolborder = new SolidColorBrush(Colors.Transparent);
        Custom objCustom = new Custom();
        public HomeValueProjection()
        {
            InitializeComponent();
            LoadPivotThemes();
            LoadAds();
        }
        private void LoadAds()
        {
            try
            {
                AdControl adc1 = new AdControl();
                adc1.AdUnitId = "31104";
                adc1.ApplicationId = "4a5d4de9-a4e2-4b73-8d01-1e2309659b46";
                adc1.Width = 480;
                adc1.Height = 80;
                adc1.BorderBrush = adcontrolborder;
                adc1.IsAutoRefreshEnabled = true;
                adstackpl.Children.Add(adc1);

            }
            catch (Exception ex)
            {
            }


        }
        private void LoadPivotThemes()
        {
            LayoutRoot.Background = Custom.getPivotBackground(); ;
        
        }
        public void LoadListPickerValues()
        {
            List<HomeValueProperties> objValueList = new List<HomeValueProperties>();
            XDocument xDoc = XDocument.Load("DispalyData/HomeValueProjection.xml");
            var values = from v in xDoc.Descendants("SpecificDate")
                         select v;
            foreach (var ele in values)
            {
                HomeValueProperties objValue = new HomeValueProperties();
                objValue.percentage = ele.Element("Percentage").Value;
                objValue.Value = ele.Element("value").Value;
                objValueList.Add(objValue);
            }
            RateList.ItemsSource = objValueList;
        }

        //private void Calculate_Click(object sender, RoutedEventArgs e)
        //{
        //    string validations=textBox1.Text;
        //    if (validations != "")
        //    {
        //        double Rate;
        //        double LowProjection;
        //        double HighProjection;
        //        double ProjectedHomeValue;
        //        double Highintrest;
        //        double LowIntrest;
        //        double value;
        //        if (Convert.ToBoolean(rbrate.IsChecked))
        //        {
        //            Rate = Convert.ToDouble((RateList.SelectedItem as HomeValueProperties).Value);
        //            Rate = Rate / 100;

        //            Highintrest = 0.0;
        //            LowIntrest = 0.0;
        //        }
        //        else if (Convert.ToBoolean(RbNAvg.IsChecked))
        //        {
        //            Rate = 0.061;
        //            Highintrest = 0.074;
        //            LowIntrest = 0.049;
        //        }
        //        else
        //        {
        //            Rate = 0.06;
        //            Highintrest = 0.0;
        //            LowIntrest = 0.0;
        //        }

        //        int Year = Convert.ToInt32((ListYear.SelectedItem as ListBoxItem).Content);
        //        value = Convert.ToDouble(textBox1.Text);
        //        ProjectedHomeValue = Convert.ToDouble(textBox1.Text);
        //        LowProjection = Convert.ToDouble(textBox1.Text);
        //        HighProjection = Convert.ToDouble(textBox1.Text);
        //        for (int i = 2006; i <= Year; i++)
        //        {
        //            ProjectedHomeValue = ProjectedHomeValue + (ProjectedHomeValue * Rate);
        //            ProjectedHomeValue = Math.Round(ProjectedHomeValue, 4);
        //            HighProjection = HighProjection + (HighProjection * Highintrest);
        //            HighProjection = Math.Round(HighProjection, 4);
        //            LowProjection = LowProjection + (LowProjection * LowIntrest);
        //            LowProjection = Math.Round(LowProjection, 4);

        //        }
        //        NavigationService.Navigate(new Uri("/Views/HomeValueProjection/ProjectionResult.xaml?phm=" + ProjectedHomeValue + "&hp=" + HighProjection + "&lp=" + LowProjection + "&value=" + value, UriKind.Relative));

        //    }
        //    else
        //    {
        //        MessageBox.Show("Please enter CurrentHome");
        //    }

        //}

        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            LoadListPickerValues();
        }

        private void ApplicationBarIconButton_Click(object sender, EventArgs e)
        {
            string validations = txtCurrentHomeValue.Text;
            if (validations != "")
            {
                double Rate;
                double LowProjection;
                double HighProjection;
                double ProjectedHomeValue;
                double Highintrest;
                double LowIntrest;
                double value;
                if (Convert.ToBoolean(rbrate.IsChecked))
                {
                    Rate = Convert.ToDouble((RateList.SelectedItem as HomeValueProperties).Value);
                    Rate = Rate / 100;

                    Highintrest = 0.0;
                    LowIntrest = 0.0;
                }
                else if (Convert.ToBoolean(RbNAvg.IsChecked))
                {
                    Rate = 0.061;
                    Highintrest = 0.074;
                    LowIntrest = 0.049;
                }
                else
                {
                    Rate = 0.06;
                    Highintrest = 0.0;
                    LowIntrest = 0.0;
                }

                int Year = Convert.ToInt32((ListYear.SelectedItem as ListBoxItem).Content);
                value = Convert.ToDouble(txtCurrentHomeValue.Text);
                ProjectedHomeValue = Convert.ToDouble(txtCurrentHomeValue.Text);
                LowProjection = Convert.ToDouble(txtCurrentHomeValue.Text);
                HighProjection = Convert.ToDouble(txtCurrentHomeValue.Text);
                for (int i = 2006; i <= Year; i++)
                {
                    ProjectedHomeValue = ProjectedHomeValue + (ProjectedHomeValue * Rate);
                    ProjectedHomeValue = Math.Round(ProjectedHomeValue, 4);
                    HighProjection = HighProjection + (HighProjection * Highintrest);
                    HighProjection = Math.Round(HighProjection, 4);
                    LowProjection = LowProjection + (LowProjection * LowIntrest);
                    LowProjection = Math.Round(LowProjection, 4);

                }
                NavigationService.Navigate(new Uri("/Views/Result/ResultPage.xaml?phm=" + ProjectedHomeValue + "&hp=" + HighProjection + "&lp=" + LowProjection + "&value=" + value, UriKind.Relative));

            }
            else
            {
                MessageBox.Show("Please enter CurrentHome");
            }
        }

        private void imgTitle_MouseEnter(object sender, MouseEventArgs e)
        {
            NavigationService.Navigate(new Uri("/Views/Result/ResultPage.xaml?sectionId=4&", UriKind.Relative));
        }
    }
}